package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {
    @Autowired
    private ShoppingCartMapper shoppingCarMapper;

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private SetmealMapper setmealMapper;

    @Autowired
    private DishFlavorMapper dishFlavorMapper;

    /**
     * 添加购物车
     *
     * @param dto
     */
    @Override
    public void addCart(ShoppingCartDTO dto) {
        //获取用户id
        Long userId = BaseContext.getCurrentId();

        //1，构造购物车对象数据
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(dto, shoppingCart);
        // 2,将购物车添加到shopping_cart表中

        //添加购物车时，需要考虑该商品是否已经存在了，如果已存在直接数量加1
        //查询购物车数据，根据什么条件：用户id+套餐id（菜品id+口味）
        shoppingCart.setUserId(userId);
        ShoppingCart cartDB = shoppingCarMapper.getBy(shoppingCart);

        if (cartDB != null) {
            cartDB.setNumber(cartDB.getNumber() + 1);
            shoppingCarMapper.update(cartDB);
        } else {
            //否则执行新增操作
            if (dto.getDishId() != null) {//代表新增的是菜品
                Dish dish = dishMapper.getById(dto.getDishId());
                shoppingCart.setName(dish.getName());
                shoppingCart.setAmount(dish.getPrice());
                shoppingCart.setImage(dish.getImage());
            } else {  //代表新增的是套餐
                Setmeal setmeal = setmealMapper.getById(dto.getSetmealId());
                shoppingCart.setName(setmeal.getName());
                shoppingCart.setAmount(setmeal.getPrice());
                shoppingCart.setImage(setmeal.getImage());
            }
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());

            shoppingCarMapper.insert(shoppingCart);
        }

    }


    /**
     *查看购物车
     * @return
     */
    @Override
    public List<ShoppingCart> list() {
        //获取当前用户id
        Long userId = BaseContext.getCurrentId();

        //调用mapper的查询方法，根据用户id查询

        return shoppingCarMapper.getByUserId(userId);
    }

    /**
     * 清空购物车
     */
    @Override
    public void clean() {
        shoppingCarMapper.deleteByUserId(BaseContext.getCurrentId());
    }

    /**
     * 根据id删除购物车中的一个商品
     * @param shoppingCartDTO
     */
    @Override
    public void subShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);

        //设置查询条件，查询当前登录用户的购物车数据
        shoppingCart.setUserId(BaseContext.getCurrentId());
        ShoppingCart cartDB = shoppingCarMapper.getBy(shoppingCart);

        if (cartDB!=null){
            Integer number = cartDB.getNumber();
            if (number==1){
                //当前商品在购物车中的份数为1，直接删除当前记录
                shoppingCarMapper.deleteById(cartDB.getId());
            }else {
                //当前商品在购物车中的分数不为1，修改份数即可
                shoppingCart.setNumber(cartDB.getNumber()-1);
                shoppingCarMapper.update(cartDB);
            }
        }

    }
}
